package com.jcfy.project.module.controller;

import com.jcfy.common.utils.SecurityUtils;
import com.jcfy.common.utils.poi.ExcelUtil;
import com.jcfy.framework.aspectj.lang.annotation.Log;
import com.jcfy.framework.aspectj.lang.enums.BusinessType;
import com.jcfy.framework.web.controller.BaseController;
import com.jcfy.framework.web.domain.AjaxResult;
import com.jcfy.framework.web.page.TableDataInfo;
import com.jcfy.project.module.domain.TDictType;
import com.jcfy.project.module.service.ITDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典类型Controller
 *
 * @author hushuai
 * @date 2020-07-06
 */
@RestController
@RequestMapping("/module/dict/type" )
public class TDictTypeController extends BaseController {
    @Autowired
    private ITDictTypeService tDictTypeService;

    /**
     * 查询字典类型列表
     */
    @PreAuthorize("@ss.hasPermi('module:dict:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(TDictType tDictType) {
        startPage();
        List<TDictType> list = tDictTypeService.selectTDictTypeList(tDictType);
        return getDataTable(list);
    }
    
    /**
     * 导出字典类型列表
     */
    @PreAuthorize("@ss.hasPermi('module:dict:export')" )
    @Log(title = "字典类型-功能模块" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(TDictType tDictType) {
        List<TDictType> list = tDictTypeService.selectTDictTypeList(tDictType);
        ExcelUtil<TDictType> util = new ExcelUtil<TDictType>(TDictType. class);
        return util.exportExcel(list, "type" );
    }

    /**
     * 获取字典类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:dict:query')" )
    @GetMapping(value = "/{dictId}" )
    public AjaxResult getInfo(@PathVariable("dictId" ) Long dictId) {
        return AjaxResult.success(tDictTypeService.selectTDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@ss.hasPermi('module:dict:add')" )
    @Log(title = "字典类型-功能模块" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDictType tDictType) {
        if (!tDictTypeService.checkTDictTypeUnique(tDictType)) {
            return AjaxResult.error("新增字典'" + tDictType.getDictName() + "'失败，字典类型已存在");
        }
        tDictType.setCreateBy(SecurityUtils.getUsername());
        return toAjax(tDictTypeService.insertTDictType(tDictType));
    }

    /**
     * 修改字典类型
     */
    @PreAuthorize("@ss.hasPermi('module:dict:edit')" )
    @Log(title = "字典类型-功能模块" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDictType tDictType) {
        if (!tDictTypeService.checkTDictTypeUnique(tDictType)) {
            return AjaxResult.error("修改字典'" + tDictType.getDictName() + "'失败，字典类型已存在");
        }
        tDictType.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(tDictTypeService.updateTDictType(tDictType));
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('module:dict:remove')" )
    @Log(title = "字典类型-功能模块" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}" )
    public AjaxResult remove(@PathVariable Long[] dictIds) {
        return toAjax(tDictTypeService.deleteTDictTypeByIds(dictIds));
    }
}
